package com.tanhua.dubbo.api;

import com.tanhua.domain.mongo.Sound;
import com.tanhua.domain.vo.SoundVo;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.List;

/**
 * @author WDY
 * @date Created in 2021/3/1 11:25
 * @modified By  WDY in 2021/3/1 11:25
 * @description
 */
@Service
public class SoundApiImpl implements SoundApi {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public void saveSound(Sound sound) {
        sound.setId(ObjectId.get());
        mongoTemplate.save(sound);
    }

    @Override
    public List<Sound> findSounds() {

        return mongoTemplate.findAll(Sound.class);
    }

    @Override
    public Sound getSoundMessage(String sid) {
        return mongoTemplate.findOne(Query.query(Criteria.where("id").is(sid)),Sound.class);
    }

 
    @Override
    public void updateState(Sound sound) {
        Query query = new Query(Criteria.where("id").is(sound.getId()));
        Update update = Update.update("state", sound.getState());
        mongoTemplate.updateFirst(query, update, Sound.class);
    }


}